import {formatDate} from '@/utils/date';

/* 数字 格式化*/
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

// 数据过滤目标内容
export function filterVal(value, list, Name = 'label', Value = 'value') {
  if (list.some((item) => { return item[Value] === value })) {
    const val = list.filter((item) => {
      return item[Value] === value
    })[0]

    return val[Name]
  }
}

// 过滤日期显示
export function formatDateTime(time, type="yyyy-MM-dd hh:mm:ss") {
  if (time == null || time === '') {
    return '';
  }
  let date = new Date(time);
  return formatDate(date, type)
}

